package com.coderxq.blog.web.contoller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.coderxq.blog.exception.WsException;
import com.coderxq.blog.utils.ResponseResult;
import com.coderxq.blog.web.entity.dto.MenuParams;
import com.coderxq.blog.web.entity.pojo.Menu;
import com.coderxq.blog.web.entity.vo.MenuInfo;
import com.coderxq.blog.web.service.menu.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: lijingming
 * @Date: 2025/2/13
 * @Description:
 **/
@RestController
@RequestMapping("/Menu")
@Api(tags = "菜单管理")
public class MenuApi {
    public static final Logger log = LoggerFactory.getLogger(MenuApi.class);


    @Autowired
    private MenuService menuService;

    @GetMapping("getRandomMenuId")
    @ApiOperation("获取一条随机的菜单Id")
    public String getRandomMenuId(){
        String snowflakeId = IdUtil.getSnowflakeNextIdStr();
        return JSONUtil.toJsonStr(ResponseResult
                .okData( snowflakeId));
    }


    @PostMapping("/addMenu")
    @ApiOperation("添加菜单")
    public Map<String, Object> addMenu(@RequestBody Menu menu) {
        Map<String, Object> result = new HashMap<>();
        Integer count = menuService.addMenu(menu);
        result.put("code", 200);
        result.put("msg", "添加成功");
        result.put("data", "成功添加" + count.toString() + "菜单");
        return result;
    }

    @PostMapping("/saveMenus")
    @ApiOperation("保存菜单")
    public String saveMenus(@RequestBody MenuParams menuParams) {
            try{
                menuService.saveMenusTreeData(menuParams.getMenusTreeData());
            }catch ( Exception e){
                 throw  new WsException(e.getMessage());
            }
        return JSONUtil.toJsonStr(ResponseResult.okMsg("保存成功"));
    }


    @GetMapping("/findAllMenu")
    public String findAllMenu() {
        Map<String, Object> result = new HashMap<>();
        ArrayList<MenuInfo> menuInfos = menuService.findAllMenu();
        ResponseResult successMap=new ResponseResult();
        successMap=ResponseResult.okData(menuInfos);
        return JSONUtil.toJsonStr(successMap);

    }

    @PostMapping("/deleteMenu")
    public Map<String, Object> deleteMenu(@RequestBody Menu menu) {
        Map<String, Object> result = new HashMap<>();
        Integer count = menuService.deleteMenu(menu.getMenuId());
        result.put("code", 200);
        result.put("msg", "删除成功");
        result.put("data", "成功删除" + count.toString() + "菜单");
        return result;
    }

    @PostMapping("/updateMenu")
    public Map<String, Object> updateMenu(@RequestBody Menu menu) {
        Map<String, Object> result = new HashMap<>();
        Integer count = menuService.updateMenu(menu);
        result.put("code", 200);
        result.put("msg", "更新成功");
        result.put("data", "成功更新" + count.toString() + "菜单");
        return result;
    }
}
